<?php

namespace app\logic;

use app\models\Department;
use yii\web\HttpException;
use app\forms\DepartmentForm;
use yii\db\StaleObjectException;
use app\components\helpers\DropListHelper;

/**
 * 部门逻辑类
 */
class DepartmentLogic extends BaseLogic
{
    /**
     * 创建部门（ 接收表单数据 ）
     * @param $param
     * @throws HttpException
     */
    public static function create($param)
    {
        $form = self::_validateForm($param, 'create');
        $attr = $form->getAttributes();
        $attr['parent_id'] = $attr['parentId'];
        $attr['group_id'] = $attr['groupId'];
        Department::newCreate($attr);
        throw new HttpException(200, 'OK');
    }

    /**
     * 编辑部门（ 接收表单数据 ）
     * @param $param
     * @throws HttpException
     */
    public static function update($param)
    {
        $form = self::_validateForm($param, 'update');
        $attr = $form->getAttributes();
        unset($attr['parentId'], $attr['groupId'], $attr['id']);
        Department::updateByPk($attr, $form['id']);
        throw new HttpException(200, 'OK');
    }

    /**
     * 删除部门（ 接收表单数据 ）
     * @param $param
     * @throws HttpException
     * @throws \Throwable
     * @throws StaleObjectException
     */
    public static function delete($param)
    {
        $form = self::_validateForm($param, 'delete');
        $model = Department::findOne($form['id']);
        if ($model->getChildrenCount() || $model->getPositionCount()) {
            throw new HttpException(423);
        }
        $model->delete();
        throw new HttpException(200);
    }

    /***
     * 某组织下的部门列表数据
     * @param $param
     * @return array
     * @throws HttpException
     */
    public static function getDropList($param)
    {
        $form = self::_validateForm($param, 'get-drop-list');
        return self::_getList($form['groupId']);
    }

    /**
     * 获取部门编辑时的信息
     * @param $param
     * @return array
     * @throws HttpException
     */
    public static function getInfo($param)
    {
        $form = self::_validateForm($param, 'get-info');
        $column = ['name', 'parent_id AS parentId', 'group_id AS groupId', 'type', 'sort'];
        $dept = Department::getSingleByPk($form['id'], $column);
        $list = self::_getList($dept['groupId']);
        $group = GroupLogic::_getList();
        $deptType = Department::getTypeMapping();
        return ['dept' => $dept, 'group' => $group, 'list' => $list, 'deptType' => $deptType];
    }

    /**
     * 获取某组织下的部门列表
     * @param $groupId
     * @return array
     */
    public static function _getList($groupId)
    {
        $deptArr = Department::getListByGroupId($groupId);
        return ((new DropListHelper())->format($deptArr));
    }

    /**
     * 参数、表单数据 验证
     * @param $param
     * @param $scenario
     * @return DepartmentForm
     * @throws HttpException
     */
    public static function _validateForm($param, $scenario)
    {
        $form = new DepartmentForm();
        $form->setScenario($scenario);
        $form->setAttributes($param);
        if (!$form->validate()) {
            throw new HttpException(400, $form->getFirstError());
        }
        return $form;
    }
}